package com.example.shiyan2222;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity4 extends AppCompatActivity {

    private ImageView[] images;
    private int currentIndex = 0;
    private Handler handler = new Handler();
    private Runnable switchRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4); // XML 布局文件名

        // 获取 ImageView 数组
        images = new ImageView[] {
                findViewById(R.id.image1),
                findViewById(R.id.image2),
                findViewById(R.id.image3),
                findViewById(R.id.image4),
                findViewById(R.id.image5)
        };

        // 图片轮播逻辑
        switchRunnable = new Runnable() {
            @Override
            public void run() {
                // 隐藏所有图片
                for (ImageView image : images) {
                    image.setVisibility(View.GONE);
                }

                // 显示当前图片
                images[currentIndex].setVisibility(View.VISIBLE);

                // 切换到下一张
                currentIndex = (currentIndex + 1) % images.length;

                // 每2秒轮换一次
                handler.postDelayed(this, 2000);
            }
        };

        handler.post(switchRunnable); // 启动轮播

        // 返回按钮逻辑
        Button buttonBack = findViewById(R.id.buttonBack);
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 关闭当前Activity，返回上一界面
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(switchRunnable); // 清除任务，避免内存泄露
    }
}
